/**
 * @description       : 使用SOQL的LIMIT和OFFSET来进行分页
 * @author            : SalesforceGogogo.com
 * @last modified on  : 2023/08/31
 * @last modified by  : SalesforceGogogo.com
**/


public with sharing class GoodPaging {
	
	// 検索結果件数
    public Integer searchCount { get; set; }
    public Integer viewrecCount { get; set; }
    
    // 検索リードリスト
    public List<Lead> searchLeadList { get; set; }              
    
    // 次ページ、前ページ用項目------------Start
    private integer totalRecs = 0;
    public integer OffsetSize = 0;
    private integer LimitSize= 50;
    
    private static final Integer PAGE_SIZE =50;     // 1ページあたりの表示件数
    public Integer currentPage {get; private set;}          // 現在選択中のページ
    public Integer totalPage {get; private set;}            // ページ数
    
    public Integer firstNum { 
        get{
            if(currentPage != null){
                return PAGE_SIZE * (currentPage - 1) + 1;
            }
            
            return 0;
        }
        set;
    }
    
    public GoodPaging(){
    	searchLead();
    }
    
    //-----------------------------------------
    // 検索
    //-----------------------------------------
    public void searchLead(){
    	searchWithPagnation();
    	
    	// ページの総数を計算
        // レコード数 / ページサイズで計算、小数部分は切り上げ
        totalPage = (Integer)Math.ceil((Decimal)searchCount / LimitSize);
        //System.debug('■totalPage---------------->'+ totalPage);
        
        currentPage = 1;
    }
    
    public void searchWithPagnation(){
        this.searchCount = 0;
        
        String leadViewQuery = 
           'Select                            ' + 
           '    Id                            ' + 
           '    ,Name                         ' + 
           '    ,Phone                        ' + 
           '    ,MobilePhone                  ' + 
           '    ,State                        ' + 
           '    ,City                         ' + 
           '    ,Street                       ' + 
           '    ,CreatedDate                  ' + 
           '    ,FirstName                    ' + 
           '    ,LastName                     ' + 
           '    ,Company                      ' + 
           '    ,ConvertedAccountId           ' +
           'From                              ' + 
           '    Lead                          ' + 
           'Where                             ' + 
           '    IsDeleted = false             ' ;
           
        String leadCntQuery = 
           'Select                            ' + 
           '    Count(Id) cnt                 ' + 
           'From                              ' + 
           '    Lead                          ' + 
           'Where                             ' + 
           '    IsDeleted = false             ' ;
        
    	// 関連データからの条件絞り込みを実行する必要がある場合は関連データから検索を行う
    	String whereStr = '';
    	leadViewQuery = leadViewQuery + whereStr + 'LIMIT :LimitSize OFFSET :OffsetSize';
    	System.debug('■leadViewQuery---------------->'+ leadViewQuery);
    	
    	
    	List<Lead> searchLeadListQuery = Database.query(leadViewQuery);
    	System.debug('■searchLeadListQuery---------------->'+ searchLeadListQuery);
    	
    	this.searchLeadList = Database.query(leadViewQuery);
    	System.debug('■searchLeadList.Size()---------------->'+ this.searchLeadList.Size());
    	
    	
    	leadCntQuery = leadCntQuery + whereStr;
        AggregateResult[] groupedResults = Database.query(leadCntQuery);
        
        // 検索結果件数を保持
        this.searchCount = Integer.valueOf(groupedResults.get(0).get('cnt'));
        //System.debug('■searchCount---------------->'+ searchCount);
    	
    	if(this.searchCount >= 50){
            this.viewrecCount = 50;
        }else{
            this.viewrecCount = this.searchCount;
        }
    }
    
    
    //Pageing対応-------------------Start
    public void FirstPage()
    {
        OffsetSize = 0;
        searchWithPagnation();
    }
    
    public void previous()
    {
        OffsetSize = OffsetSize - LimitSize;
        searchWithPagnation();
        currentPage--;
    }
    
    public void next()
    {
        OffsetSize = OffsetSize + LimitSize;
        searchWithPagnation();
        currentPage++;
    }
    
    public void LastPage()
    {
        OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
        searchWithPagnation();
    }
    
    //前のページがあるかどうか
    public boolean getenablePrev()
    {
        if(OffsetSize == 0 || currentPage == 1){
            return false;
        }else{
            return true;
        }
    }
    
    //次のページがあるかどうか
    public boolean getenableNext()
    {
        if((OffsetSize + LimitSize) > totalRecs){
            return false;
        }else{
            return true;    
        }
    }
    //Pageing対応-------------------End
}
